<?php

namespace App\Providers;

use App\Services;
use Illuminate\Http\Request;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->singleton('cipher', 'App\Services\Cipher\\'.config('secure.cipher'));
        $this->app->singleton(Services\Option::class);
        $this->app->alias(Services\Option::class, 'options');
        $this->app->singleton(Services\Webpack::class);
    }

    public function boot(Request $request)
    {
        $this->configureUrlGenerator($request);
    }

    /**
     * Control the URL generated by url() function.
     *
     * @codeCoverageIgnore
     */
    protected function configureUrlGenerator(Request $request): void
    {
        if (!option('auto_detect_asset_url')) {
            $rootUrl = option('site_url');

            // Replace HTTP_HOST with site_url set in options,
            // to prevent CDN source problems.
            if ($this->app['url']->isValidUrl($rootUrl)) {
                $this->app['url']->forceRootUrl($rootUrl);
            }
        }

        /**
         * Check whether the request is secure or not.
         * True is always returned when "X-Forwarded-Proto" header is set.
         *
         * We define this function because Symfony's "Request::isSecure()" method
         * needs "setTrustedProxies()" which sucks when load balancer is enabled.
         */
        $isRequestSecure = $request->server('HTTPS') === 'on'
            || $request->server('HTTP_X_FORWARDED_PROTO') === 'https'
            || $request->server('HTTP_X_FORWARDED_SSL') === 'on';

        if (option('force_ssl') || $isRequestSecure) {
            $this->app['url']->forceScheme('https');
        }
    }
}
